package com.hlwxy.ssm.service.impl;

import com.hlwxy.ssm.dao.SystemUserMapper;
import com.hlwxy.ssm.model.SystemUser;
import com.hlwxy.ssm.service.SystemUserService;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("systemUserService")
public class SystemUserServiceImpl implements SystemUserService {

    @Autowired
    private SystemUserMapper systemUserMapper;

    public List<SystemUser> findDemoUserByPage(SystemUser systemUser) {
        return systemUserMapper.findDemoUserByPage(systemUser);
    }

    /**
     * 分页查询用户数据
     * @param systemUser
     * @return
     */
    public List<SystemUser> findSystemUserByPage(SystemUser systemUser) {
        return systemUserMapper.findSystemUser(systemUser);
    }

    public Integer findSystemUserCount(SystemUser systemUser) {
        return systemUserMapper.findSystemUserCount(systemUser);
    }

    public void register(SystemUser systemUser) {
        systemUserMapper.addSystemUser(systemUser);
    }

    public void mailAuthentication(SystemUser systemUser) {
        systemUser.setState(1);
        systemUserMapper.updateSystemUser(systemUser);
    }

    public void sendEmail(String url,String email, String userId) {
        HtmlEmail htmlEmail = new HtmlEmail();
        htmlEmail.setHostName("smtp.qq.com");// 设置使用发电子邮件的邮件服务器，这里以qq邮箱为例（其它例如：【smtp.163.com】，【smtp.sohu.com】）
        try {
            // 收件人邮箱
            htmlEmail.addTo(email);
            // 邮箱服务器身份验证
            htmlEmail.setCharset("UTF-8");
            htmlEmail.setAuthentication("54925881@qq.com", "otylqeobcgxscbbg");
            // 发件人邮箱
            htmlEmail.setFrom("54925881@qq.com");
            // 邮件主题
            htmlEmail.setSubject("邮箱注册功能");
            // 邮件内容
             url=url+"/user/authentication?id="+userId;
            htmlEmail.setHtmlMsg("<a href='"+url+"'>点击如下链接完成认证</a>");
            // 发送邮件
            htmlEmail.send();

        } catch (EmailException ex) {
            ex.printStackTrace();
        }
    }

    public SystemUser getSystemUser(SystemUser systemUser) {
        return systemUserMapper.getSystemUser(systemUser);
    }

    public void update(SystemUser systemUser) {
        systemUserMapper.updateSystemUser(systemUser);
    }
}
